package com.dmbjz.thymeleaf.controller;


import com.dmbjz.thymeleaf.entity.Person;
import com.dmbjz.thymeleaf.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.List;

@Controller
public class ThymeleafController {


    @GetMapping(value = "welcome")
    public ModelAndView welcome(){

        ModelAndView modelAndView = new ModelAndView();

        //需要添加的参数，可以使用 $() 获取到具体结果
        modelAndView.addObject("content","需要显示的内容");
        modelAndView.addObject("url","http://www.baidu.com");
        modelAndView.addObject("logo","隧道logo");

        //使用 th:object 方式搭配 *() 获取对象中的参数
        Student student = new Student("dmbjz",18);
        modelAndView.addObject("student",student);

        //添加一串HTML代码
        modelAndView.addObject("inputHTML","<span style='color:red'>你好</span>");

        //设置需要跳转的页面名称。页面路径：前缀（spring.thymeleaf.prefix） + 视图名 + 后缀（spring.thymeleaf.suffix）
        modelAndView.setViewName("welcome");

        //添加图片参数
        modelAndView.addObject("img1","/images/logo.png");
        modelAndView.addObject("img2","/images/logo2.png");
        modelAndView.addObject("style1","width:40px");
        modelAndView.addObject("style2","width:40px");

        //指定是否需要显示内容
        modelAndView.addObject("showInfo",false);

        return modelAndView;

    }

    @GetMapping(value = "path")
    public ModelAndView path(){

        ModelAndView modelAndView = new ModelAndView();

        //设置需要跳转的页面名称。页面路径：前缀（spring.thymeleaf.prefix） + 视图名 + 后缀（spring.thymeleaf.suffix）
        modelAndView.setViewName("path");

        //添加图片参数，当有 server.servlet.context-path 这类配置时，和 URL 有关的参数都需要使用 @{} 来获取
        // @{} 会自动加项目访问路径，使用 ${} 无法在有 server.servlet.context-path 配置时正常获取到资源文件
        modelAndView.addObject("logoPng","/images/logo.png");

        return modelAndView;

    }

    @GetMapping(value = "method")
    public ModelAndView method(){

        ModelAndView modelAndView = new ModelAndView();

        //添加的参数
        modelAndView.addObject("content","this is info");

        //设置需要跳转的页面名称
        modelAndView.setViewName("method");

        return modelAndView;

    }

    @GetMapping(value ="theach")
    public ModelAndView theach(){

        List<Person> personList = Arrays.asList(
                new Person(1L, "张三1", "100@163.com", 10, "pm"),
                new Person(2L, "张三2", "200@163.com", 20, "pm"),
                new Person(3L, "张三3", "300@163.com", 30, "pm"),
                new Person(4L, "张三4", "400@163.com", 40, "admin"),
                new Person(5L, "张三5", "500@163.com", 50, "boss"),
                new Person(6L, "张三6", null, 50, "boss")
        );

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("personList",personList);
        modelAndView.setViewName("method");
        return modelAndView;

    }


}
